package c.x.jy.wexin.controller;

import c.x.jy.common.annotation.Login;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.R;
import c.x.jy.common.utils.StringTools;
import c.x.jy.dao.entity.MemberEntity;
import c.x.jy.dao.entity.ServiceEntity;
import c.x.jy.dao.entity.TeacherEntity;
import c.x.jy.dao.entity.TeacherServiceEntity;
import c.x.jy.service.service.MemberService;
import c.x.jy.service.service.ServiceService;
import c.x.jy.service.service.TeacherService;
import c.x.jy.service.service.TeacherServiceService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 大师高级项目配置表
 *
 * @author amwhyh
 * @email xxx@xx.com
 * @date 2021-05-24 10:50:04
 */
@RestController
@RequestMapping("wechat/teacherservice")
public class TeacherServiceController {
    @Autowired
    private TeacherServiceService teacherServiceService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ServiceService serviceService;

    //获取专家服务项目列表，根据teacher id
    @Login
    @RequestMapping("/getByTeacherId")
    public R getByTeacherId(Long teacherId){
        TeacherServiceEntity teacherServiceEntity = null;
        MemberEntity memberEntity = memberService.getCurrUser();
        TeacherEntity teacherEntity = teacherService.getByMemberId(memberEntity.getId());
        if(teacherEntity == null) return R.put(null);
        List<TeacherServiceEntity> list = teacherServiceService.getByTeacherId(teacherEntity.getId());
        return R.put(list);
    }
    @Login
    @Transactional
    @RequestMapping("/updateServiceList")
    public R updateServiceList(@RequestBody List<TeacherServiceEntity> services){
        Long memberId = memberService.getCurrUser().getId();
        TeacherEntity teacherEntity = teacherService.getByMemberId(memberId);
        Long teacherId = teacherEntity.getId();
        String spec="";
        ServiceEntity serviceEntity;
        for(int i=0;i<services.size();i++){
            serviceEntity = serviceService.getById(services.get(i).getServiceId());
            if(StringUtils.isEmpty(spec)){
                spec=serviceEntity.getTitle();
            }else{
                spec = spec+","+serviceEntity.getTitle();
            }
            services.get(i).setMemberId(memberId);
            services.get(i).setTeacherId(teacherId);
            services.get(i).setCreateId(memberId);
        }

//        services.forEach((item)->{
//            item.setMemberId(memberId);
//            item.setTeacherId(teacherId);
//            item.setCreateId(memberId);
//
//
//        });
        teacherEntity.setSpecialty(spec);
        teacherService.updateById(teacherEntity);
        teacherServiceService.updateServiceList(services);

        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("teacher:teacherservice:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = teacherServiceService.queryPage(params);

        return R.put(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("teacher:teacherservice:info")
    public R info(@PathVariable("id") Long id){
		TeacherServiceEntity teacherService = teacherServiceService.getById(id);

        return R.put(teacherService);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("teacher:teacherservice:save")
    public R save(@RequestBody TeacherServiceEntity teacherService){
		teacherServiceService.save(teacherService);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("teacher:teacherservice:update")
    public R update(@RequestBody TeacherServiceEntity teacherService){
		teacherServiceService.updateById(teacherService);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("teacher:teacherservice:delete")
    public R delete(@PathVariable("id") Long id){
		teacherServiceService.removeByIds(Arrays.asList(id));

        return R.ok();
    }

}

